package com.xstudio.controllers;

import com.sun.javafx.scene.control.skin.VirtualFlow;
import com.xstudio.dao.*;
import com.xstudio.models.*;
import com.xstudio.mybatis.Pagination;
import com.xstudio.utilities.CommonUtility;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by kira on 16/2/28.
 */
@Controller
@RequestMapping(value = "/admin/dict")
public class DictManageController {
    String prefix = "admin/dict/";

    @Resource
    SysDictMapper sysDictMapper;

    @Resource
    SysDictMapperExtend sysDictMapperExtend;

    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request, Model model) {
        Pagination pagination = CommonUtility.getPagination(request, model);
        HashMap<String, String> map = CommonUtility.getParameterMap(request);
        CommonUtility.renderGridData(request, model,
                sysDictMapperExtend.all(pagination, map),
                pagination);

        return prefix + "index";
    }

    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, Model model) {
        return prefix + "add";
    }

    @RequestMapping(value = "/addEntity")
    @ResponseBody
    public String addEntity(HttpServletRequest request, Model model, SysDict sysDict) {
        sysDictMapper.insertSelective(sysDict);
        return "";
    }

    @RequestMapping(value = "/edit")
    public String edit(HttpServletRequest request, Model model, Integer id) {
        model.addAttribute("entity", sysDictMapper.selectByPrimaryKey(id));
        return prefix + "edit";
    }

    @RequestMapping(value = "/editEntity")
    @ResponseBody
    public String editEntity(HttpServletRequest request, Model model, SysDict sysDict) {
        sysDictMapper.updateByPrimaryKeySelective(sysDict);
        return "";
    }

    @RequestMapping(value = "/deleteEntity")
    @ResponseBody
    public String deleteEntity(HttpServletRequest request, Model model, Integer id) {
        sysDictMapper.deleteByPrimaryKey(id);
        return "";
    }
}
